<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>通用盘点分析</title>
    <script type="text/javascript">
      Ext.onReady(function(){

        var productTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">','<div style="float:left; width:220px">{name}</div><div style="float:left; width:90px">{number}</div>{is_suit}','</div></tpl>');
        var productStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/product/getAllProducts?for_cbb=true_simple"
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }, {
              name: 'is_suit',
              convert:function(value,record){
                if(value == "1"){
                  return "套装";
                }else if(value == "0"){
                  return "单品";
                }
              }
            }])
        });

        //供应商产品
        var productCBB = new Ext.form.ComboBox({
          store: productStore,
          fieldLabel: '产品',
          blankText: "产品必须选择",
          emptyText: '请选择...',
          //typeAhead: false,
          loadingText: '搜索中...',
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'product_id',
          anchor: '95%',
          pageSize: 10,
          //hideTrigger: false,
          tpl: productTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          selectOnFocus:true
        });

        //供应商相关combobox
        var vendorTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{name}', '</div></tpl>');
        var vendorStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/vendor/getAllVendors?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'name'
            }])
        });

        //供应商
        var vendor = new Ext.form.ComboBox({
          store: vendorStore,
          fieldLabel: '供应商',
          blankText: "供应商必须选择",
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'vendor_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
          emptyText: '请选择...',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '95%',
          //pageSize: 10,
          //hideTrigger: false,
          readOnly:false,
          tpl: vendorTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction: 'all',
          editable:false,
          selectOnFocus:true
        });

        vendor.on('select', function(combo, record, index) {
          vendorGroup.disable();
        });

        //所有仓库
        var warehouseTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{number}|{name}', '</div></tpl>');
        var warehouseStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/warehouse/getAllWarehouse?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            }, {
              name: 'name'
            }])
        });
        
        var warehouse = new Ext.form.ComboBox({
          store: warehouseStore,
          fieldLabel: '选择仓库',
          blankText: "仓库必须选择",
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'warehouse_id',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '95%',
          //pageSize: 10,
          tpl: warehouseTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction:'all',
          selectOnFocus:true
        });      

        warehouse.on('select', function(combo, record, index) {
          warehouseGroup.disable();
        });

        //所有盘点日期
        var inventoryDateTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{action_date}', '</div></tpl>');
        var inventoryDateStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/inventory/getAllInventoryDate"
          }),
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'action_date',
              type: 'date',
              dateFormat: 'Y/m/d',
              convert:function(value,record){
                return Ext.util.Format.date(value,'Y-m-d');
              }
            }])
        });

        var inventoryDate = new Ext.form.ComboBox({
          store: inventoryDateStore,
          fieldLabel: '盘点日期',
          allowBlank: false,
          blankText: "盘点日期必须选择",
          emptyText: '请选择...',
          valueField: 'action_date',
          displayField: 'action_date',
          hiddenName: 'action_date',
          //typeAhead: false,
          loadingText: '搜索中...',
          anchor: '95%',
          tpl: inventoryDateTemplate,
          minChars: 0,
          itemSelector: 'div.search-item',
          triggerAction:'all',
          selectOnFocus:true
        });   

        var vendorGroup = new Ext.form.Checkbox({
          name: 'vendor_group',
          clearCls: 'allow-float',
          itemCls: 'float-left',
          fieldLabel: '供应商分组'
        })

        var warehouseGroup = new Ext.form.Checkbox({
          name: 'warehouse_group',
          clearCls: 'allow-float',
          itemCls: 'float-left',
          fieldLabel: '仓库分组'
        })

        var submitButton = new Ext.Button({
          text: '执行统计',
          iconCls: 'finding',
          handler: function(){
            if (queryFormPanel.form.isValid()) {
              statsResultDS.removeAll();
              statsResultDS.baseParams.vendor_id = vendor.getValue();
              statsResultDS.baseParams.warehouse_id = warehouse.getValue();
              if (vendor.getValue()==""){
                statsResultDS.baseParams.vendor_group = vendorGroup.getValue();
              }
              if (warehouse.getValue()==""){
                statsResultDS.baseParams.warehouse_group = warehouseGroup.getValue();
              }              
              statsResultDS.baseParams.action_date = inventoryDate.getRawValue();
              statsResultDS.baseParams.product_id = productCBB.getValue();
              statsResultDS.load({callback: function(record, options, success){
                  if (success == false){
                    alert("您没有权限执行此操作！");
                  }else{
                    amountLabel.setText("总金额:"+Ext.util.Format.number(statsResultDS.reader.jsonData.amount,'0,000.00'));
                    glAmountLabel.setText("损益总金额:"+Ext.util.Format.number(statsResultDS.reader.jsonData.gl_amount,'0,000.00'));
                  }
                }
              });
            }
          }
        });

        var clearButton = new Ext.Button({
          text: '清空',
          width:70,
          handler: function(){
            queryFormPanel.form.reset();
            vendorGroup.enable();
            warehouseGroup.enable();
          }
        });
        
        var amountLabel = new Ext.form.Label({
          name: 'amount_label'
        })

        var glAmountLabel = new Ext.form.Label({
          name: 'gl_amount_label'
        })

        amountLabel.setText("总金额:0");
        glAmountLabel.setText("损益总金额:0");

        var queryFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          //renderTo: 'query_form',
          labelWidth: 55,
          width: screenWidth-175,
          labelAlign: 'right',
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 1200},
              title: '统计条件',
              border: true,
              items: [{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .18,
                      layout: 'form',
                      border: false,
                      labelWidth: 65,
                      items: [vendor]
                    }, {
                      columnWidth: .18,
                      layout: 'form',
                      border: false,
                      items: [warehouse]
                    }, {
                      columnWidth: .4,
                      layout: 'form',
                      border: false,
                      items: [productCBB]
                    }]
                },{
                  layout: 'column',
                  border: false,
                  items: [{
                      columnWidth: .18,
                      layout: 'form',
                      border: false,
                      labelWidth: 65,
                      items: [vendorGroup]
                    }, {
                      columnWidth: .18,
                      layout: 'form',
                      border: false,
                      items: [warehouseGroup]
                    },{
                      columnWidth: .14,
                      layout: 'form',
                      border: false,
                      items: [inventoryDate]
                    }, {
                      columnWidth: .09,
                      layout: 'form',
                      border: false,
                      items: [submitButton]
                    }, {
                      columnWidth: .1,
                      layout: 'form',
                      border: false,
                      items: [clearButton]
                    }]
                }]
            }]
        });

        //显示销售订单操作时的窗口介面。
        //----------------------------------销售订单Grid-------------------------
        //销售订单Grid Store
        var statsResultDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize + 10
          },
          proxy: new Ext.data.HttpProxy({
            url: '/inventory/getInventoryInfoForCommonReport'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'vendor_name'
            },{
              name: 'warehouse_name'
            },{
              name: 'product_name'
            },{
              name: 'product_number'
            },{
              name: 'quantity',
              type: 'int'
            },{
              name: 'gl_quantity',
              type: 'int'
            },{
              name: 'cost_price',
              type: 'float'
            },{
              name: 'amount',
              type: 'float'
            },{
              name: 'gl_amount',
              type: 'float'
            }])
        });

        //生成销售订单Grid的CM
        var statsResultSM = new Ext.grid.CheckboxSelectionModel({singleSelect:true});
        var statsResultCM = new Ext.grid.ColumnModel({defaults: {
            sortable: true,
            menuDisabled: false
          },columns:[new Ext.grid.RowNumberer(),statsResultSM,{
              id: 'common',
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '供应商',
              dataIndex: 'vendor_name',
              width: 170
            },{
              header: '仓库',
              dataIndex: 'warehouse_name',
              width: 170
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 270
            },{
              header: '编号',
              dataIndex: 'product_number',
              width: 70
            },{
              header: '盘点数量',
              dataIndex: 'quantity',
              align:'right',
              width: 75
            },{
              header: '均价',
              dataIndex: 'cost_price',
              align:'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 75
            },{
              header: '金额',
              dataIndex: 'amount',
              align:'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 75
            },{
              header: '损益数量',
              dataIndex: 'gl_quantity',
              align:'right',
              width: 75
            },{
              header: '损益金额',
              dataIndex: 'gl_amount',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              align:'right',
              width: 75
            }
          ]});

        //生成要订单的产品的Grid
        var statsResultGrid = new Ext.grid.GridPanel({
          title:'统计结果列表',
          store: statsResultDS,
          cm: statsResultCM,
          sm: statsResultSM,
          loadMask: true,
          width: screenWidth-175,
          height: screenHeight-380,
          frame: true,
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
          bbar: [new Ext.PagingToolbar({
              pageSize: pageSize + 10,
              store: statsResultDS,
              displayInfo: true,
              displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
              emptyMsg: "没有记录"
            }),'->','-',amountLabel,'-',glAmountLabel,'-']
        });    

        //总的panel
        var workbenchPanel = new Ext.Panel({
          width: screenWidth-175,
          height: screenHeight-250,
          frame:false,
          layout:'table',
          layoutConfig: {columns:1},
          defaults:{margins:'5 5 5 5'},
          items:[queryFormPanel,statsResultGrid]
        });
        workbenchPanel.render("common_inventory_status_workbench_panel");
      })
    </script>
  </head>
  <body>
    <div id="common_inventory_status_workbench_panel"></div>
  </body>
</html>